MyMap = {}
/**
	 *在地图上显示指定车辆的GPS数据
	 */
MyMap.showVehicleOnMap = function (deviceId) {
	//调用ajax方法获得车辆的实时数据
	var url = path + "/realData/refreshRealData";
	var me = this;
	$.getJSON(url, { dvcId: deviceId }, function (result) {
		if (result.success == false)
			return;
		//登录
		if (result.logoutFlag) {
			window.top.location = path + "/login";
			return false;
		}
		var rd = result.data; //GPS实时数据
		//mytree.fillGrid(rd);//填充到车辆树底部的车辆属性表格中
		var deviceInfo = me.convertRecord(rd);
		var mapHandler = me.getMapHandler();
		if (mapHandler == null)
			return;
		mapHandler.centerMarkerOverlays([deviceInfo], true, true); //调用map/js/base.js的方法

	});
}

MyMap.createMarkerByGpsData = function (gpsdata) {
	var mapHandler = this.getMapHandler();
	this.clearMarkerByGpsData();
	mapHandler.showMarkers(gpsdata);
}


//批量清除地图车辆图标
MyMap.clearMarkerByGpsData = function () {
	var mapHandler = this.getMapHandler();
	mapHandler.clearMarkers();
}

//点击设备清除地图车辆图标
MyMap.clearMarkerByDevice = function (sim) {
	var mapHandler = this.getMapHandler();
	mapHandler.clearMarkerDevice(sim);
}

//清除地图的实时车辆地标
MyMap.clearAllVehicle = function () {
	var mapHandler = this.getMapHandler();
	mapHandler.clearAllVehicle();
}


//得到地图监控窗口中的地图对象
MyMap.getMapHandler = function () {
	var iframeWindow = $("#mapFrame")[0];
	if (iframeWindow)
		return iframeWindow.contentWindow.OperatorObj; //得到地图页面上的javascript 地图对象，便于主界面调用
	return null;
}

function getStartPOIData(keyword) {
	if (keyword.length < 2)
		return;

	MyMap.getMapHandler().keyword = keyword;

	MyMap.startPOISearch.search(keyword, { forceLocal: true });
}
function getEndPOIData(keyword) {
	if (keyword.length < 4)
		return;
	MyMap.getMapHandler().keyword = keyword;

	MyMap.endPOISearch.search(keyword, { forceLocal: true });
}

/**
 * 初始化地点搜索输入框的autocomplete控件，以来jquery.autocomplete.js
 */
MyMap.initAutoComplete = function () {
	var me = this;
	var startPOI = $("#startPOI");
	startPOI.autocomplete('url', {
		dataCallback: getStartPOIData,
		minChars: 2,
		max: 12,
		autoFill: false,
		mustMatch: false,
		matchContains: false,
		scrollHeight: 220,
		formatItem: function (data, i, total) {
			var name = data.title + ", " + data.address;
			return name;
		}
	}).result(function (event, data, formatted) {
		me.startPoint = data;
	});

	var endPOI = $("#endPOI", window.parent.parent.document);
	endPOI.autocomplete('url', {
		dataCallback: getEndPOIData,
		minChars: 0,
		max: 12,
		autoFill: false,
		mustMatch: false,
		matchContains: true,
		scrollHeight: 220,
		formatItem: function (data, i, total) {
			var name = data.title + ", " + data.address;
			return name;
		}
	}).result(function (event, data, formatted) {
		me.endPoint = data;
	});

	this.startPOISearch = this.getMapHandler().createLocalSearch(function (term, data) {
		startPOI.fillData(term, data);
	});
	this.endPOISearch = this.getMapHandler().createLocalSearch(function (term, data) {
		endPOI.fillData(term, data);
	});

	$("#btnPlanRoute").click(function () {
		me.getMapHandler().planDriveRoute(me.startPoint, me.endPoint);

	});
}



MyMap.convertRecord = function (rd) {
	rd.latitude = parseFloat(rd.latitude).toFixed(6);
	rd.longitude = parseFloat(rd.longitude).toFixed(6);
	rd.speed = parseFloat(rd.speed).toFixed(1);
	rd.altitude = parseInt(rd.altitude);
	rd.course = parseInt(rd.course);
	var deviceInfo = {id:rd.carid, text:rd.simno, rLat:rd.latitude,rLng:rd.longitude, tLat:rd.latitude, tLng:rd.longitude,status:rd.status,direction:rd.course,
		velocity:rd.speed, online:rd.status};

	var merged = $.extend({}, rd, deviceInfo);
	return merged;

}


